package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.EmployeePerformanceMapper;
import com.ruoyi.business.domain.EmployeePerformance;
import com.ruoyi.business.service.IEmployeePerformanceService;

import javax.validation.Validator;

/**
 * 员工年度绩效Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-18
 */
@Service
public class EmployeePerformanceServiceImpl  extends BaseServiceImpl implements IEmployeePerformanceService
{
    @Autowired
    private EmployeePerformanceMapper employeePerformanceMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询员工年度绩效
     * 
     * @param id 员工年度绩效主键
     * @return 员工年度绩效
     */
    @Override
    public EmployeePerformance selectEmployeePerformanceById(Long id)
    {
        return employeePerformanceMapper.selectEmployeePerformanceById(id);
    }

    /**
     * 查询员工年度绩效列表
     * 
     * @param employeePerformance 员工年度绩效
     * @return 员工年度绩效
     */
    @Override
    public List<EmployeePerformance> selectEmployeePerformanceList(EmployeePerformance employeePerformance)
    {
        return employeePerformanceMapper.selectEmployeePerformanceList(employeePerformance);
    }

    /**
     * 新增员工年度绩效
     * 
     * @param employeePerformance 员工年度绩效
     * @return 结果
     */
    @Override
    public int insertEmployeePerformance(EmployeePerformance employeePerformance)
    {
        employeePerformance.setCreateTime(DateUtils.getNowDate());
        return employeePerformanceMapper.insertEmployeePerformance(employeePerformance);
    }

    /**
     * 修改员工年度绩效
     * 
     * @param employeePerformance 员工年度绩效
     * @return 结果
     */
    @Override
    public int updateEmployeePerformance(EmployeePerformance employeePerformance)
    {
        employeePerformance.setUpdateTime(DateUtils.getNowDate());
        return employeePerformanceMapper.updateEmployeePerformance(employeePerformance);
    }

    /**
     * 批量删除员工年度绩效
     * 
     * @param ids 需要删除的员工年度绩效主键
     * @return 结果
     */
    @Override
    public int deleteEmployeePerformanceByIds(Long[] ids)
    {
        return employeePerformanceMapper.deleteEmployeePerformanceByIds(ids);
    }

    /**
     * 删除员工年度绩效信息
     * 
     * @param id 员工年度绩效主键
     * @return 结果
     */
    @Override
    public int deleteEmployeePerformanceById(Long id)
    {
        return employeePerformanceMapper.deleteEmployeePerformanceById(id);
    }

    @Override
    public String importData(List<EmployeePerformance> performanceList, boolean updateSupport, String operName) {
    if (performanceList == null || performanceList.isEmpty()) {
        throw new ServiceException("导入员工绩效数据不能为空！");
    }

    int successNum = 0;
    int failureNum = 0;
    StringBuilder successMsg = new StringBuilder();
    StringBuilder failureMsg = new StringBuilder();
        List<String>failureNums = new ArrayList<>();

        int i = 1;
    for (EmployeePerformance performance : performanceList) {
        try {
            i++;
            if (performance.getAwardYear() == null || performance.getAwardYear().equals("") || performance.getEmployeeId() ==null || performance.getEmployeeId().equals("") || !isAwardYearValid(performance.getAwardYear())) {
                failureNums.add(String.valueOf(i));
                failureNum++;
                continue ;
            }
            // 验证是否存在这个员工绩效
            EmployeePerformance employeePerformance = new EmployeePerformance();
            employeePerformance.setEmployeeId(performance.getEmployeeId());
            employeePerformance.setAwardYear(performance.getAwardYear());
            List<EmployeePerformance> selectEmployeePerformanceList = employeePerformanceMapper.selectEmployeePerformanceList(employeePerformance);

            if (true) {
                // 验证数据
                BeanValidators.validateWithException(validator, performance);
                // 设置初始值
                performance.setCreateTime(DateUtils.getNowDate());
                performance.setCreateBy(operName);
                employeePerformanceMapper.insertEmployeePerformance(performance);
                successNum++;
                //successMsg.append("<br/>" + successNum + "、员工绩效 " + " 导入成功");
            } else if (true) {
                // 验证数据
                EmployeePerformance existingPerformance = selectEmployeePerformanceList.get(0);
                BeanValidators.validateWithException(validator, performance);
                performance.setId(existingPerformance.getId());
                performance.setUpdateTime(DateUtils.getNowDate());
                performance.setUpdateBy(operName);
                employeePerformanceMapper.updateEmployeePerformance(performance);
                successNum++;
                //successMsg.append("<br/>" + successNum + "、员工绩效 "  + " 更新成功");
            } else {
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、员工绩效 "  + " 已存在");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、员工绩效 "  + " 导入失败：";
            failureMsg.append(msg + e.getMessage());
        }
    }

    if (failureNum > 0) {
        failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确");
        //throw new ServiceException(failureMsg.toString());
    } else {
        successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
    }

        setCacheInfo("员工绩效数据",String.valueOf(successNum),String.valueOf(failureNum),failureNums.stream().collect(Collectors.joining(",")));

    return successMsg.toString() + failureMsg.toString();
}

}
